package com.yanqu.road.server.cmd.activity.zfb;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.reids.subpub.RedisReliableMessageSubPub;
import com.yanqu.road.reids.subpub.eRedisChannelType;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigInteger;
import java.util.List;

@CmdApi()
@Cmd(code = Protocol.S_ZHI_FU_BAO_INDEX_SETTING, desc = "支付宝活动中心设置首页")
public class ZfbIndexSettingCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(!SystemOpenMgr.systemOpen(player, eSystemId.ZfbIndexSetting.getValue())){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        List<ActivityInfo> list = NormalActivityMgr.getOpenActivityInfoList(eActivityType.Zfb.getValue(), 1);
        if(list.size() == 0){
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        ActivityInfo activityInfo = list.get(0);
        if(!ActivityMgr.activityInTime(activityInfo)){
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        BaseActivityData activityData = player.getModule(ActivityModule.class).getBaseActivityData(activityInfo.getActivityId());
        UserActivityConditionData conditionData = activityData.getFirstUserActivityConditionData();
        if(conditionData.getValue().compareTo(BigInteger.ZERO) == 0){
            //通知redis
            UserInfo userInfo = UserMgr.getUserInfo(player.getUserId());
            if(userInfo != null) {
                String message = userInfo.getPlatformUserId() + ";" + eRedisChannelType.ZfbIndex.getType();
                RedisReliableMessageSubPub.getInstance().publish(eRedisChannelType.ZfbIndex, message);
            }
        }
        player.notifyListener(eGamePlayerEventType.ZfbIndexSetting.getValue(), 1);
    }

    @Override
    public void sendData(GamePlayer player) {
        player.sendPacket(Protocol.U_ZHI_FU_BAO_INDEX_SETTING, CommonProto.ResultValueMsg.newBuilder().setRet(ret));
    }
}
